<?php

@session_start();
// just require TCPDF instead of FPDF
$userid = $_SESSION[ses_user_name];
$fullname = $_SESSION[ses_full_name];
header("Content-Type:application/vnd.adobe.pdf");
require_once('tcpdf.php');
require_once('fpdi.php');
require_once('../cls/cls.php');
require_once('../cls/hrclass.php');
$hr = new hrclass;
$con = new connect_base;
$con->connect();

if ($flag == 1) {
    $send_date = $_GET[send_date];
    $send_date = $hr->change_datetobaseY($send_date);
    $d = $hr->get_date("$send_date", "d");
    $m = $hr->get_date("$send_date", "m");
    $y = $hr->get_date("$send_date", "y");
    $nowdate = $d . " " . $m . " " . $y;
} else {
    $reportDate1 = $_GET[reportdate1];
    $reportDate2 = $_GET[reportdate2];
    $reportDate1 = $hr->change_datetobaseY($reportDate1);
    $d = $hr->get_date("$reportDate1", "d");
    $m = $hr->get_date("$reportDate1", "m");
    $y = $hr->get_date("$reportDate1", "y");
    $report_Date1 = $d . " " . $m . " " . $y;

    $reportDate2 = $hr->change_datetobaseY($reportDate2);
    $d = $hr->get_date("$reportDate2", "d");
    $m = $hr->get_date("$reportDate2", "m");
    $y = $hr->get_date("$reportDate2", "y");
    $report_Date2 = $d . " " . $m . " " . $y;
    $nowdate = $report_Date1 . " ถึงวันที่ " . $report_Date2;
}

class PDF extends TCPDF {

    function setVar($value1, $value2) {
        $this->var1 = $value1;
        $this->var2 = $value2;
    }

    function Header() {

        $this->SetFont("thsarabun", "B", 16);
        $header = '<table width="500" border="0" cellspacing="0" cellpadding="0"><thead>
  <tr>
    <td height="33" colspan="3">เทศบาลนครอุดรธานี (ผู้ปฏิบัติงาน : ' . $this->var2 . ')</td>
    <td colspan="1" align="right">วันที่ ' . $this->var1 . '</td>
  </tr>
   <tr >
    <td width="50" height="30" align="center"  >ลำดับที่</td>
    <td width="300" align="center" >ประเภทเงิน</td>
    <td width="150" align="center" >จำนวนเงิน</td>
  </tr>
 </thead></table><hr>';
        $this->writeHTML('<br><div align="center">ใบสำคัญสรุปใบนำส่งเงิน</div>' . $header, true, false, false, false, '');
    }

    function Footer() {
        
    }

}

//=======================================================================
//$send_date=$_GET[send_date];
//========================================================================
// initiate PDF
$pdf = new PDF();
$pdf->setVar($nowdate, $fullname);
$pdf->SetMargins(PDF_MARGIN_LEFT, 38, PDF_MARGIN_RIGHT);
$pdf->SetAutoPageBreak(true, 20);
//$pdf->setFontSubsetting(false);
// add a page
$pdf->AddPage();

// get esternal file content
//$utf8text = file_get_contents("cache/utf8test.txt", true);
//=========================================================================== Plot PDF
$pdf->SetFont("thsarabun", "", 14);
$sql = "select * from money where money_id=1";
$rsmoney = $hr->run_sql_select($sql);


$tbl_header = '<table width="500" border="0" cellspacing="0" cellpadding="0">';
//$tbl_footer = '</table>';
$tbl = '';
$total = 0;

for ($n = 0; $n < count($rsmoney); $n++) {
    $sql0 = "select * from money1 where money_id=" . $rsmoney[$n]['money_id'] . " and money1_id=2";
    $rs0 = $hr->run_sql_select($sql0);
    $tbl.='<tr><td width="500" align="left" colspan=3><br><br><u>' . $rsmoney[$n]['money_name'] . '</u></td></tr>';

    for ($y = 0; $y < count($rs0); $y++) {
        $sql = "select * from money2 where money1_id=" . $rs0[$y]['money1_id'];
        $rs = $hr->run_sql_select($sql);
        $tbl.='<tr><td width="500" align="left" colspan=3><br>&nbsp;&nbsp;' . $rs0[$y]['money1_name'] . '</td></tr>';
        for ($i = 1; $i <= count($rs); $i++) {
            $a = $i - 1;
            if ($flag == 1) {
                $sql = "select sum(bill_value) from bill where money2_id=" . $rs[$a][money2_id] . " and confirm>=1 and send_date='$send_date' and user_id='$userid' and bill_status='T'";
            } else {
                $sql = "select sum(bill_value) from bill where money2_id=" . $rs[$a][money2_id] . " and confirm>=1 and send_date between '$reportDate1' and '$reportDate2' and user_id='$userid' and bill_status='T'";
            }
            $rssum = $hr->run_sql_select($sql);
            $total += $rssum[0][0];
            $tbl.='<tr><td width="50" align=center>' . $i . '</td><td align=left width="350">' . $rs[$a][money2_name] . '</td><td width="100" align="right">' . number_format($rssum[0][0], 2) . '</td></tr>';
        }
    }
}
$tbl.="</table><hr>";

$tbl_sum = '<table width="500" border="0" cellspacing="0" cellpadding="0" ><tr><td align="left" width="400"><b>รวมจำนวนเงิน</b></td>
    <td align="right" width="100" ><u><b>' . number_format($total, 2) . '</b></u></td></tr></table>';
$pdf->writeHTML($tbl_header . $tbl . $tbl_sum, true, false, false, false, '');
$pdf->lastPage();
$total1 = $total;
$pdf->AddPage();
$sql = "select * from money_out where lvl1_id='" . $_SESSION[ses_lvl1_id] . "'";
$rs = $hr->run_sql_select($sql);
$tbl_header = '<table width="641" border="0" cellspacing="0" cellpadding="0">';
//$tbl_footer = '</table>';
$tbl = '';
$total = 0;
$tbl.='<tr><td width="500" align="left" colspan=3><br><u>เงินนอกงบประมาณ</u></td></tr>';
for ($i = 1; $i <= count($rs); $i++) {
    $a = $i - 1;
    if ($flag == 1) {

        $sql = "select sum(bill_value) from bill1 where money2_id=" . $rs[$a][money_out_id] . " and confirm>=1 and send_date='$send_date' and bill_status='T'";
    } else {

        $sql = "select sum(bill_value) from bill1 where money2_id=" . $rs[$a][money_out_id] . " and confirm>=1 and send_date between '$reportDate1' and '$reportDate2' and bill_status='T'";
    }
    $rssum = $hr->run_sql_select($sql);
    $total += $rssum[0][0];
    $tbl.='<tr><td width="50">' . $i . '</td><td align=left width="240">' . $rs[$a][money_out_name] . '</td><td width="80" align="right">
    ' . $rs[$a][e_id] . '</td><td width="130" align="right">' . number_format($rssum[0][0], 2) . '</td></tr>';
}
$tbl.="</table><hr>";

$tbl_sum = '<table width="500" border="0" cellspacing="0" cellpadding="0" ><tr><td align="left" width="400"><b>รวมจำนวนเงิน</b></td>
    <td align="right" width="100" ><u><b>' . number_format($total, 2) . '</b></u></td></tr></table>';
$sumtotal = $total1 + $total;
$tbl_sum.='<br><table width="500" border="0" cellspacing="0" cellpadding="0" ><tr><td align="left" width="400"><b>รวมจำนวนเงินสุทธิ
     (เงินในงบประมาณ + เงินนอกงบประมาณ)</b></td>
    <td align="right" width="100" ><u><b>' . number_format($sumtotal, 2) . '</b></u></td></tr></table>';
$pdf->writeHTML($tbl_header . $tbl . $tbl_sum, true, false, false, false, '');
$pdf->lastPage();
$pdf->Output("pdf", 'I');
?>